package com.atguigu.demojuc.chap07;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueDemo {

    public static void main(String[] args) throws InterruptedException {

        BlockingQueue<String> synchronousQueue = new SynchronousQueue<>();
        /*synchronousQueue.put("a"); //不存储元素的阻塞队列
        System.out.println(synchronousQueue.take()); //null*/

         new Thread(()->{
             try {
                 for (int i = 0; i < 10; i++) {
                     String data = UUID.randomUUID().toString().substring(0, 8);
                     System.out.println("生产: " + data);
                     synchronousQueue.put(data);
                 }

             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }, "生产者线程").start();


         new Thread(()->{
                 try {
                     for (int i = 0; i < 10; i++) {
                         String data = synchronousQueue.take();
                         System.out.println(Thread.currentThread().getName()
                                 + " 消费: " + data);
                     }

                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
         }, "消费者线程").start();
    }
}